package com.lw.smartdevice.service.impl;

import com.lw.smartdevice.cache.SceneCache;
import com.lw.smartdevice.enums.CheckType;
import com.lw.smartdevice.exceptions.AddMyBaseException;
import com.lw.smartdevice.exceptions.MyErrorCode;
import com.lw.smartdevice.exceptions.QueryMyBaseException;
import com.lw.smartdevice.mapper.SysSceneMapper;
import com.lw.smartdevice.pojo.SysScene;
import com.lw.smartdevice.pojo.SysUserInfo;
import com.lw.smartdevice.service.SysSceneService;
import com.lw.smartdevice.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/21 21:29
 */
@Service
@Transactional
public class SysSceneServiceImpl implements SysSceneService {

    private SceneCache sceneCache;

    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    private SysSceneMapper sysSceneMapper;

    @Autowired
    public void setSysSceneMapper(SysSceneMapper sysSceneMapper) {
        this.sysSceneMapper = sysSceneMapper;
    }

    @Override
    public void addSysScene(SysScene sysScene) {
        Assert.isTrue(!sysScene.isEmpty(CheckType.ADD),()->{
            throw new AddMyBaseException("场景名称没有传递", MyErrorCode.DATA_NULL);
        });
        //判断登录
        SysUserInfo userInfo = SecurityUtils.getLoginUser();
        Assert.notNull(userInfo,()->{
            throw new AddMyBaseException("未登录",MyErrorCode.NOT_LOGIN);
        });
        //我们要求同一个用户同名的场景只能出现一个
        //根据用户名和场景名字查询，如果能查询到则代表存在（检查是否标记为删除），如果不存在，则添加
        SysScene scene = sysSceneMapper.findByIdAndName(userInfo.getUId(), sysScene.getSceneName());
        //如果数据已经存在，但是是删除的状态，我们就更新，如果不存在就是添加，否则就抛出异常
        Assert.isTrue(scene==null||scene.getStatus()==0,()->{
            throw new AddMyBaseException("场景已经存在",MyErrorCode.DATA_ALREADY_EXIST);
        });

        if(scene==null){
            //补全数据
            sysScene.setCreateBy(userInfo.getUId());
            sysScene.setCreateTime(new Date());
            //添加数据
            sysSceneMapper.addSysScene(sysScene);
        }else {
            //更新数据
            sysSceneMapper.updateScene(scene.getSceneId(),1L);
        }

    }

    @Override
    public SysScene findById(Long id) {
        SysUserInfo userInfo = SecurityUtils.getLoginUser();
        Assert.notNull(userInfo,()->{
            throw new QueryMyBaseException("未登录",MyErrorCode.NOT_LOGIN);
        });
        //查询数据的时候需要根据id和当前的用户进行查询
        //方式1 查询数据库的条件就是主键和用户的id

        //方式2  根据主键查询数据, 然后代码中判断用户
        //从缓存中查询
        SysScene sysScene = sceneCache.get(id);
        Assert.isTrue(sysScene!=null&&sysScene.getCreateBy()==userInfo.getUId(),()->{
            throw new QueryMyBaseException("数据不存在",MyErrorCode.DATA_NULL);
        });
        return sysScene;
    }
}
